package com.tiancheng.trade.order.feign;

import com.tiancheng.trade.commom.web.http.PlatformRequest;
import com.tiancheng.trade.commom.core.data.Result;
import com.tiancheng.trade.commom.core.utils.JsonUtil;
import com.tiancheng.trade.order.feign.dto.InvoiceDTO;
import com.tiancheng.trade.order.feign.dto.InvoiceRedDTO;
import com.tiancheng.trade.order.util.CommonUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 电子发票系统
 *
 * @author kellen
 * @date 2024/6/26
 */
@Slf4j
@Component
public class InvoiceSystemService {

    @Resource
    private PlatformRequest platformRequest;

    @Value(value = "${system.invoice.domain}")
    private String invoiceDomain;

    /**
     * 开具蓝票
     * @param invoiceDTO
     * @return
     */
    public String blue(InvoiceDTO invoiceDTO) {
        invoiceDTO.setSerialNo(CommonUtil.uuid());
        log.info("开具发票:{}", JsonUtil.toJsonString(invoiceDTO));

        Result result = platformRequest.jsonPostRequest(invoiceDomain + "/api/blue", invoiceDTO, true, null);
        log.info("开具发票返回:{}", result);

        Map<String, Object> resultMap = JsonUtil.fromJson(result.toString(), Map.class);
        Map<String, Object> dataMap = (Map<String, Object>) resultMap.get("data");
        return String.valueOf(dataMap.get("invoiceSn"));
    }

    /**
     * 待开具发票
     * @param invoiceDTO
     * @return
     */
    public String createBlue(InvoiceDTO invoiceDTO) {
        invoiceDTO.setSerialNo(CommonUtil.uuid());
        log.info("待开具发票:{}", JsonUtil.toJsonString(invoiceDTO));

        Result result = platformRequest.jsonPostRequest(invoiceDomain + "/api/waitBlue", invoiceDTO, true, null);
        log.info("待开具发票返回:{}", result);

        Map<String, Object> resultMap = JsonUtil.fromJson(result.toString(), Map.class);
        Map<String, Object> dataMap = (Map<String, Object>) resultMap.get("data");
        return String.valueOf(dataMap.get("invoiceSn"));
    }

    /**
     * 提交开具发票
     * @param invoiceDTO
     * @return
     */
    public boolean submitBlue(InvoiceDTO invoiceDTO) {
        log.info("提交开具发票:{}", JsonUtil.toJsonString(invoiceDTO));

        Result result = platformRequest.jsonPostRequest(invoiceDomain + "/api/createBlue", invoiceDTO, true, null);
        log.info("提交开具发票返回:{}", result);
        return true;
    }

    /**
     * 冲红发票
     * @param channel
     * @param invoiceSn
     * @param orderId
     * @return
     */
    public boolean red(String channel, String invoiceSn, String orderId) {
        InvoiceRedDTO invoiceRedDTO = new InvoiceRedDTO();
        invoiceRedDTO.setChannel(channel);
        invoiceRedDTO.setSerialNo(CommonUtil.uuid());
        invoiceRedDTO.setOrders(Arrays.asList(new InvoiceRedDTO.Order(invoiceSn, orderId)));

        log.info("发票冲红:{}", JsonUtil.toJsonString(invoiceRedDTO));

        Result result = platformRequest.jsonPostRequest(invoiceDomain + "/api/red", invoiceRedDTO, true, null);
        log.info("发票冲红返回:{}", result);

        Map<String, Object> resultMap = JsonUtil.fromJson(result.toString(), Map.class);
        if(resultMap.containsKey("data") && ((List)resultMap.get("data")).size() > 0) {
            return false;
        }
        return true;
    }

    /**
     * 重新开具发票
     * @param invoiceDTO
     * @return
     */
    public boolean reset(InvoiceDTO invoiceDTO) {
        log.info("重新开具发票:{}", JsonUtil.toJsonString(invoiceDTO));

        Result result = platformRequest.jsonPostRequest(invoiceDomain + "/api/remakeInvoice", invoiceDTO, true, null);
        log.info("重新开具发票返回:{}", result);

        return true;
    }

    /**
     * 更新待开票状态的信息
     * @param invoiceDTO
     * @return
     */
    public boolean update(InvoiceDTO invoiceDTO) {
        log.info("更新待开具发票信息:{}", JsonUtil.toJsonString(invoiceDTO));

        Result result = platformRequest.jsonPostRequest(invoiceDomain + "/api/updateBlueItems", invoiceDTO, true, null);
        log.info("更新待开具发票信息返回:{}", result);
        return true;
    }
}
